package grpc.demo;

import com.google.protobuf.ByteString;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.TimeUnit;

/**
 * @author Rocket
 * @version 10.0.1
 * @date 2021-09-06 6:02 PM
 */
public class HelloServer {

    private Server server;

    private void start() throws IOException {
        int port = 10051;
        server = ServerBuilder.forPort(port)
                .maxInboundMessageSize(2099999999)
                .addService(new HelloImpl())
                .build().start();

        System.out.println("Server started.");

        Runtime.getRuntime().addShutdownHook(new Thread(()-> {
            System.err.println("shutting down gRPC server since JVM is shutting down.");
            try {
                HelloServer.this.stop();
            } catch(InterruptedException e) {
                e.printStackTrace(System.err);
            }
        }));
    }

    private void stop() throws  InterruptedException {
        if(server != null) {
            server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
        }
    }

    private void blockUntilShutdown() throws InterruptedException {
        if(server != null) {
            server.awaitTermination();
        }
    }

    private class HelloImpl extends HelloGrpc.HelloImplBase {
        @Override
        public void sayHello(Demo2.HelloRequest request, StreamObserver<Demo2.HelloResponse> responseObserver) {
            Demo2.HelloResponse helloResponse = Demo2.HelloResponse.newBuilder()
                    .setMessage("こんにちは！ " + request.getName() + ", 我是gRPC服务端.").build();
            ByteString bs = request.getPhoto();


            try {
                FileOutputStream fos = new FileOutputStream("c:/tmp/" + System.currentTimeMillis() + ".oop");
                bs.writeTo(fos);
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            responseObserver.onNext(helloResponse);
            responseObserver.onCompleted();
        }

        @Override
        public void updateProtocol(Demo2.InstanceRequest request, StreamObserver<Demo2.InstanceResponse> responseObserver) {
            Demo2.InstanceResponse instRes = Demo2.InstanceResponse.newBuilder()
                    .setName("http change").setMessage("Finished.").build();
            String protocol = request.getProtocol();
            if("https".equals(protocol)) {
                // update db
            }
            //do business
            responseObserver.onNext(instRes);
            responseObserver.onCompleted();
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        HelloServer server = new HelloServer();
        server.start();
        server.blockUntilShutdown();
    }
}
